$('document').ready(function() {
    
    $('.sf_admin_pagination a').on('click', function() {
        $.get(
            $(this).attr('href'),
            {}, 
            function(data, status) {
                if(status == 'success') {
                    $('.sf_admin_list').html(data);
                }
            }, 
            'html'
            );
        return false;
    });
    
    $('.sf_admin_list thead a').on('click', function() {
        $.get(
            $(this).attr('href'),
            {}, 
            function(data, status) {
                if(status == 'success') {
                    $('.sf_admin_list').html(data);
                }
            },'html');
        return false;
    });
    
    $('#sf_admin_filter form').on('submit', function() {
        $.post(
            $(this).attr('action'),
            $(this).serialize(),
            function(data, status) {
                if(status == 'success') {
                    $('.sf_admin_list').html(data);
                }
            }, 'html');
        return false;
    });
    
    $('#sf_filter_reset').on('click', function() {
        $.get(
            $(this).attr('href'),
            {}, 
            function(data, status) {
                if(status == 'success') {
                    $('.sf_admin_list').html(data);
                    $('#sf_admin_filter form')[0].reset();
                }
            },'html');
        return false;
    });
    
    $('#ajax_search_reset').on('click', function() {
        $.get(
            $('#ajax_search_action').val() + '?_search&k',
            {},
            function(data, status) {
                if(status == 'success') {
                    $('#ajax_search').val('');
                    $('.sf_admin_list').html(data);
                }
            }, 'html');
        return false;
    });
    
    $('#ajax_search').on('keyup', function(key) {
        if(key.which == 13) {
            $.get(
                $('#ajax_search_action').val() + '?_search&k=' + $(this).val(),
                {},
                function(data, status) {
                    if(status == 'success') {
                        $('.sf_admin_list').html(data);
                    }
                }, 'html');
            return false;
        }
    });
    
});

function deleteSuccess(data) {
    message = 'El elemento ha sido eliminado';
    $('#sf_admin_container h1').after(
    '<div class="alert alert-success fade in">' +
    '<button type="button" class="close" data-dismiss="alert">×</button>' +
    message +
    '</div>');
}

function deleteError(data) {
    message = 'El elemento no se pudo eliminar';
    $('#sf_admin_container h1').after(
    '<div class="alert alert-error fade in">' +
    '<button type="button" class="close" data-dismiss="alert">×</button>' +
    message +
    '</div>');
}